(in-package :lilu-app-example)

#|
(defun make-and-register-browser (name version-script make-args-fn)
  (let ((app (make-instance 'program-application
                            :name name
                            :version-script version-script
                            :make-args-fn make-args-fn
                            :tags '(:browser))))
    (register-application app)))


(make-and-register-browser "firefox2"
                           "firefox --version | awk '{print $3}'"
                           #'(lambda (application &optional url &rest args)
                               (if url
                                   (list url)
                                   nil)))



(defun test-01 (url)
  (let ((app01 (make-program-application :firefox
                                         "firefox2"
                                         "firefox --version | awk '{print $3}'"
                                         #'(lambda (application &optional url &rest args)
                                                (if url
                                                    (list url)
                                                    nil)))))
    #+nil(format *debug-io* "==>~A~%" (application-version app01))
    #+nil(run-application app01 (list url)
                     :error-p t
                     :wait-p t
                     :callback-fn #'(lambda (app args ret)
                                      (format *debug-io* "==>~A ~A ~A~%" app args ret)))
  ))
|#

(defclass terminal-application (program-application)
  ((cmd-single-p :initarg :cmd-single-p))
  (:default-initargs
   :make-args-fn #'(lambda(app &optional cmd)
                     (with-slots (cmd-single-p) app
                       (if cmd
                           (uiop:with-temporary-file (:stream strm :pathname file :keep t)
                             (format strm (uiop:escape-sh-command cmd))
                             (format strm "~%rm ~a" (uiop:unix-namestring file))
                             :close-stream
                             (if cmd-single-p
                                 (list "-e"
                                       (format nil "sh ~a" (uiop:unix-namestring file)))
                                 (list "-e" "sh"
                                       (format nil "~a" (uiop:unix-namestring file)))))
                           nil)))))

(defun make-terminal-application (string-designator
                                  program
                                  version-script
                                  cmd-single-p)
  (make-instance 'terminal-application
                 :name (string string-designator)
                 :program program
                 :version-script version-script
                 :cmd-single-p cmd-single-p
                 :tags '(:terminal)))

(defun register-terminal (string-designator
                          program
                          version-script
                          cmd-single-p)
  (register-application
   (make-terminal-application string-designator program
                              version-script cmd-single-p)))
(register-terminal :xfce
                   "xfce4-terminal"
                   "xfce4-terminal --version | head -1 | awk '{print $2}'"
                   t)

(register-terminal :cool-retro
                   "cool-retro-term"
                   " cool-retro-term --version 2>&1 | awk '{ print $2 }'"
                   nil)
